package com.mywork.util;

import javax.servlet.http.HttpServletRequest;
import java.net.*;
import java.util.Enumeration;

public class IPUtil {


    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");

        String ip2 = request.getHeader("X-Forwarded-For");
        if (ip2 != null && (!"unknown".equalsIgnoreCase(ip2))) {
            ip = ip2;
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("http_client_ip");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        // 如果是多级代理，那么取第一个ip为客户ip
        if (ip != null && ip.contains(",")) {
            ip = ip.substring(ip.lastIndexOf(",") + 1, ip.length()).trim();
        }
        return ip;
    }

    private IPUtil() {

    }

    public static String getLinuxLocalIP() throws SocketException {
        String ip = "";
        Enumeration<NetworkInterface> e1 = NetworkInterface.getNetworkInterfaces();
        while (e1.hasMoreElements()) {
            NetworkInterface ni = e1.nextElement();
            String name = ni.getName();
            if (!name.contains("docker") && !name.contains("lo")) {
//			if (("eth0".equals(ni.getName())) || ("bond0".equals(ni.getName())))
                Enumeration<InetAddress> e2 = ni.getInetAddresses();
                while (e2.hasMoreElements()) {
                    InetAddress ia = e2.nextElement();
                    if (!(ia instanceof Inet6Address)) {
                        ip = ia.getHostAddress();
                    }
                }
            }
        }
        return ip;
    }

    public static String getWinLocalIP() throws UnknownHostException {
        return InetAddress.getLocalHost().getHostAddress();
    }

    public static String getLocalIP() {
        try {
            if (!System.getProperty("os.name").contains("Win")) {
                return getLinuxLocalIP();
            } else {
                return getWinLocalIP();
            }
        } catch (Exception e) {
            return "Unknown IP";
        }
    }

    public static String formatId(String Ip) {
        try {
            if (StringUtil.isEmpty(Ip)) {
                return "";
            }
            if (Ip.length() > 20) {
                Ip = Ip.contains(",") ? Ip.split(",")[0] : Ip;
            }
            if (Ip.length() > 20) {
                Ip = Ip.substring(0, 20);
            }
        } catch (Exception e) {

        }
        return Ip;
    }
}
